﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        uint my_ref = 1;
        bool RCB3_Connected = false;
        int RCB3_Home_Position_Reference = 0;
        uint[] out_homePositions = new uint[24];
        uint[] Current_Servo_Position = new uint[24];
        uint[] Default_Home_Position = new uint [24]{   16410,16809,16664,16368,16384,16378,16384,16384,
                                                        16384,16384,16384,16384,16384,16384,16384,16384,
                                                        16384,16384,16384,16384,16384,16384,16384,16384
                                                    };
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (!RCB3_Connected)
            {
                button1.Text = "disconnect";
                button1.BackColor = Color.Red;
                if (RCB3Interface.CreateRCB3Interface(3, 115200, 0, ref my_ref))
                {
                    RCB3_Connected = true;
                    StringBuilder version = new StringBuilder(65);
                    //char[] version = new char[65];
                    if (RCB3Interface.GetRCBVersion(my_ref, version))
                    {
                        textBox1.Text = "RCB3 Version:" + version;

                    }

                   
                    if (RCB3Interface.GetPortsHomePosition(my_ref, out_homePositions/*24*/, 2))
                    {
                        ServoList.Items.Clear();
                        for (int i = 0; i < 24; i++)
                        {
                            ServoList.Items.Add(new ListViewItem(string.Format("Servo {0:D} = {1:D}\n", i, out_homePositions[i]), 0));

                            // listBox1.Items.Add(new lis   string.Format("Servo {0:D} = {1:D}\n", i, out_homePositions[i]));
                        }
                    }
                    //   RCB3Interface.DestroyRCB3Interface(my_ref);
                }
                
 
            }
            else
            {
                button1.Text = "connect";
                button1.BackColor = Color.Green;
                RCB3Interface.DestroyRCB3Interface(my_ref);
                RCB3_Connected = false;
                ServoList.Items.Clear();
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int16 k = 0x09;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ServoList.SelectedItems.Count == 1)
            {
                MessageBox.Show(ServoList.SelectedItems[0].Text);
            }
        }

        private void readADC_Click(object sender, EventArgs e)
        {
            float adc1 = 1, adc2 = 1, adc3 = 1, power = 5;
            if (RCB3Interface.GetAnalogInputs(my_ref, ref power, ref adc1, ref adc2, ref adc3))
            {
                stMenuADC_1.Text = adc1.ToString();
                stMenuADC_2.Text = adc2.ToString();
                stMenuADC_3.Text = adc3.ToString();
            }
            else
            {
                MessageBox.Show("adc get error");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (RCB3Interface.SetPortSpeedAndPosition(my_ref, 0, 100, out_homePositions[0] - 100, 1))
                MessageBox.Show("dondu");
            else
            {
                MessageBox.Show("olmadı");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (RCB3Interface.SetPortSpeedAndPosition(my_ref, 0, 100, out_homePositions[0] + 100, 1))
                MessageBox.Show("dondu");
            else
            {
                MessageBox.Show("olmadı");
            }
        }

        private void trServo0_Scroll(object sender, EventArgs e)
        {
           
            if (Set_ServoPositionAndSpeed(0,trServo0.Value,10))
            {
                lblServo0_value.Text = Current_Servo_Position[0].ToString();
            }
            else
            {
                MessageBox.Show("Servo 0 position error");
            }

        }

        private void trServo1_Scroll(object sender, EventArgs e)
        {
            if (Set_ServoPositionAndSpeed(1, trServo1.Value, 10))
            {
                lblServo1_value.Text = Current_Servo_Position[1].ToString();
            }
            else
            {
                MessageBox.Show("Servo 1 position error");
            }
        }

        private void trServo2_Scroll(object sender, EventArgs e)
        {
            if (Set_ServoPositionAndSpeed(2, trServo2.Value, 10))
            {
                lblServo2_value.Text = Current_Servo_Position[2].ToString();
            }
            else
            {
                MessageBox.Show("Servo 2 position error");
            }
        }

        private void trServo3_Scroll(object sender, EventArgs e)
        {
            if (Set_ServoPositionAndSpeed(3, trServo3.Value, 10))
            {
                lblServo3_value.Text = Current_Servo_Position[3].ToString();
            }
            else
            {
                MessageBox.Show("Servo 3 position error");
            }
        }

        private void trServo5_Scroll(object sender, EventArgs e)
        {
            if (Set_ServoPositionAndSpeed(5, trServo5.Value, 10))
            {
                lblServo5_value.Text = Current_Servo_Position[5].ToString();
            }
            else
            {
                MessageBox.Show("Servo 5 position error");
            }
        }

        private void btnSetHome_Click(object sender, EventArgs e)
        {
            ServoList.Items.Clear();
            for (uint i = 0; i < 24; i++)
            {
                ServoList.Items.Add(new ListViewItem(string.Format("Servo {0:D} = {1:D}\n", i,Default_Home_Position[i]), 0));
                
                if (!RCB3Interface.SetPortSpeedAndPosition(my_ref,i,10,Default_Home_Position[i],i ))
                {
                    MessageBox.Show("Error While setting home positions");
                    break;
                }

            }
            trServo0.Value = Convert.ToInt16(Default_Home_Position[0])-Convert.ToInt16( out_homePositions[0]);
            trServo1.Value = Convert.ToInt16(Default_Home_Position[1])-Convert.ToInt16( out_homePositions[1]);
            trServo2.Value = Convert.ToInt16(Default_Home_Position[2])-Convert.ToInt16( out_homePositions[2]);
            trServo3.Value = Convert.ToInt16(Default_Home_Position[3])-Convert.ToInt16( out_homePositions[3]);
            trServo5.Value = Convert.ToInt16(Default_Home_Position[5])-Convert.ToInt16( out_homePositions[5]);
           
        }
        private void Refresh_ServoList()
        {
            //if(
            //for (uint i = 0; i < 24; i++)
            //{

            //}
        }
        private bool Set_ServoPositionAndSpeed(uint i, int value,uint speed)
        {
            if (RCB3_Home_Position_Reference == 0)
            {
                Current_Servo_Position[i] = Convert.ToUInt16(Convert.ToInt16(Default_Home_Position[i]) + value);
                if (RCB3Interface.SetPortSpeedAndPosition(my_ref, i, speed, Current_Servo_Position[i], 1))
                    return true;
                else
                    return false;
            }
            else if (RCB3_Home_Position_Reference == 1)
            {
                Current_Servo_Position[i] = Convert.ToUInt16(Convert.ToInt16(out_homePositions[i]) + value);
                if (RCB3Interface.SetPortSpeedAndPosition(my_ref, i, speed, Current_Servo_Position[i], 1))
                    return true;
                else
                    return false;
            }
            else
                return false;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            RCB3_Home_Position_Reference = 0;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            RCB3_Home_Position_Reference = 1;
        }






    }
}
